package cn.wolfcode.luowowo.website.config;

import cn.wolfcode.luowowo.website.web.interceptor.LoginInterceptor;
import cn.wolfcode.luowowo.website.web.interceptor.RateInterceptor;
import cn.wolfcode.luowowo.website.web.resolver.UserInfoArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public RateInterceptor rateInterceptor() {
        return new RateInterceptor();
    }

    /**
     * 配置自定义拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/regist.html")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/styles/**")
                .excludePathPatterns("/static/**");


//        registry.addInterceptor(rateInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/login.html")
//                .excludePathPatterns("/regist.html")
//                .excludePathPatterns("/images/**")
//                .excludePathPatterns("/js/**")
//                .excludePathPatterns("/styles/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userInfoArgumentResolver());
    }

    @Bean
    public UserInfoArgumentResolver userInfoArgumentResolver() {
        return new UserInfoArgumentResolver();
    }

}
